<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>event</title>
    </head>
    <body>
        <div id="root">
            <div id="parent">
                <p id="child">
                  事件执行顺序
                </p>
              </div>
        </div>
        <script>
        let root = document.getElementById('root');
        let parent = document.getElementById('parent');
        let child = document.getElementById('child');

        root.addEventListener('click',event=>dispatchEvent(event,true),true);
        root.addEventListener('click',event=>dispatchEvent(event,false));
        function dispatchEvent(event,isCapture){
            let paths = [];
            let current = event.target;
            while(current){
                paths.push(current);
                current=current.parentNode;
            }
            if(isCapture){
              for(let i=paths.length-1;i>=0;i--){
                let eventHandler = paths[i].onClickCapture;
                eventHandler&&eventHandler()
              }
            }else{
              for(let i=0;i<paths.length;i++){
                let eventHandler = paths[i].onClick;
                eventHandler&&eventHandler()
               }
            }
        }

        parent.addEventListener("click", () => {
          console.log("父元素原生捕获");
        },true);
        parent.addEventListener("click", () => {
          console.log("父元素原生冒泡");
        });
        child.addEventListener("click", () => {
          console.log("子元素原生捕获");
        },true);
        child.addEventListener("click", () => {
          console.log("子元素原生冒泡");
        });
        document.addEventListener('click',()=>{
            console.log("document原生捕获");
        },true);
        document.addEventListener('click',()=>{
            console.log("document原生冒泡");
        });
        parent.onClick=() => {
            console.log("父元素React事件冒泡");
        }
        parent.onClickCapture=() => {
            console.log("父元素React事件捕获");
        }
        child.onClick=() => {
            console.log("子元素React事件冒泡");
        }
        child.onClickCapture=() => {
            console.log("子元素React事件捕获");
        }
    </script>
    </body>
</html>